<?php

class EditCompanyController extends Controller {
    public function actionIndex() {
        $detect = new Mobile_Detect;
        if ($detect->isMobile()) {
            $this->layout = '//layouts/main_mobile';
        }
        if (!isset($_GET['id'])) throw new CHttpException(999, 'Invalid request');

        $company = Company::model()->findByPk($_GET['id']);
        $model = new EditCompany();

        $model->company_id = $company->id;
        $model->name = $company->name;
        $model->owner = $company->owner;
        $model->address = $company->address;
        $model->city = $company->city;
        $model->published = 0;
        $model->phone = $company->phone;
        $model->category_id = $company->category_id;
        $model->picture = $company->picture;

        if ($model == null) throw new CHttpException(999, 'Invalid request');

        if (isset($_POST['EditCompany'])) {
            $pic_url = $model->picture;
            $model->attributes = $_POST['EditCompany'];
            $file = CUploadedFile::getInstance($model, 'picture');
            $year = date('Y');
            $month = date('m');
            $day = date('d');

            if ($file != null) {
                $imageVariables = getimagesize($file->tempName);
                $file = EUploadedImage::getInstance($model, 'picture');
                if ($imageVariables[0] < Yii::app()->params['picture-width']) {
                    $model->addError('piture', 'Vui lòng chọn hình ảnh có chiều dài ít nhất là ' . Yii::app()->params['picture-width'] . 'px');
                    return $this->render('index', array(
                        'model' => $model,
                        'categories' => Category::model()->findAll()
                    ));
                }
                $extension = $file->getExtensionName();
                $fileName = HSCommon::getSEO($model->name) . '_' . rand(10000, 99999) . '.' . $extension;

                $base = HSCommon::checkFolderExisted($year, $month, $day, 'company');

                if ($file->saveAs($base . "/$year/$month/$day/$fileName")) {
                    $model->picture = "/pictures/company/$year/$month/$day/$fileName";

                    if ($model->save()) {
                        Yii::app()->user->setFlash('success', "Thông tin chỉnh sửa đã được gửi, vui lòng chờ kiểm duyệt.");
                        $this->redirect(HSUrl::home());
                    }
                } else {
                    $model->addError('picture', 'Không lưu được ảnh');
                }

            } else {
                $model->picture = $pic_url;
                if ($model->save()) {
                    Yii::app()->user->setFlash('success', "Thông tin chỉnh sửa đã được gửi, vui lòng chờ kiểm duyệt.");
                    $this->redirect(HSUrl::home());
                }
            }
        }

        $this->render('index', array(
            'model' => $model,
            'categories' => Category::model()->findAll()
        ));
    }

}